package exa.unicen.edu.ringo.raccess.resources;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import exa.unicen.edu.ringo.raccess.model.manager.ConfigurationManager;
import exa.unicen.edu.ringo.rmodel.bean.Configuration;
import exa.unicen.edu.ringo.rmodel.bean.Service;
import exa.unicen.edu.ringo.rmodel.builder.ModelBuilder;

@Path("/services")
public class ServiceResource {

	private ModelBuilder builder = ModelBuilder.instance;
	private ConfigurationManager manager = ConfigurationManager.instance;
	
	@POST
	@Consumes({"application/xml","text/xml"})
	@Produces({"application/xml","text/xml"})
	public String set(String xml) {
		Service service = (Service) builder.marshal(Service.class, xml);
		
		Configuration configuration = manager.get();
		configuration.services.put(service.id, service);
		
		return xml;
	}	
	
	@GET
	@Path("/{id}")
	@Produces({"application/xml","text/xml"})
	public String get(@PathParam("id") String id) {
		
		Configuration configuration = manager.get();
		Service service = configuration.services.get(id);

		return builder.unmarshal(Service.class, service);
	}
	
	@DELETE
	@Path("/{id}")
	@Produces({"application/xml","text/xml"})
	public String delete(@PathParam("id") String id) {
		
		Configuration configuration = manager.get();
		Service service = configuration.services.get(id);
		
		configuration.services.remove(id);
		
		return builder.unmarshal(Service.class, service);
	}	
	
}
